package com.xzakota.minesweeper.ui.dialog;

import com.xzakota.minesweeper.Client;
import com.xzakota.minesweeper.contant.ConstantPool;
import com.xzakota.minesweeper.msg.*;
import com.xzakota.minesweeper.ui.dialog.templae.AbstractBaseDialog;
import com.xzakota.minesweeper.ui.dialog.templae.MessageDialog;
import com.xzakota.minesweeper.ui.dialog.templae.SwingWorkerWaitDialog;
import com.xzakota.minesweeper.util.JSONFileUtils;

import java.awt.*;
import java.util.concurrent.CountDownLatch;

/**
 * 登出对话框
 *
 * @author Xzakota
 */
public class SignOutDialog extends AbstractBaseDialog {
    public SignOutDialog(Window parent) {
        super(parent);

        setTitle("退出登录");
        setMessage("真的要离我而去吗？😭\n");

        setConfirmButton("确认");
        setConfirmAction(e -> {
            SwingWorkerWaitDialog waitDialog = new SwingWorkerWaitDialog();
            waitDialog.setTipText("正在退出登录中...");
            waitDialog.worker = new SwingWorkerWaitDialog.Worker() {
                @Override
                public Boolean doInBackground() {
                    SignOutMsg sendMsg = new SignOutMsg();
                    sendMsg.data = Client.getClient().currentUser;

                    final boolean[] result = {false};

                    CountDownLatch latch = new CountDownLatch(1);
                    waitDialog.doReleaseEvent();
                    waitDialog.tempThread = new Thread(() -> {
                        Client client = Client.getClient();
                        while (client.socketState != 1 && !Thread.currentThread().isInterrupted()) {
                        }
                        MsgHandler handler = new MsgHandler() {
                            @Override
                            public void handle(BaseMsg msg) {
                                if (msg.code == MsgCode.BASE_SUCCESS) {
                                    result[0] = true;
                                }
                                new MessageDialog(SignInMsgDefaultHandler.resultMsgText).showWindow();
                                MsgHandlerFactory.removeMsgHandler(MsgHandlerFactory.MSG_TYPE_SIGN_OUT, this);
                                latch.countDown();
                            }
                        };
                        MsgHandlerFactory.addMsgHandler(MsgHandlerFactory.MSG_TYPE_SIGN_OUT, handler);
                        Client.getClient().sendMsg(sendMsg);
                    });
                    waitDialog.tempThread.start();

                    try {
                        latch.await();
                    } catch (InterruptedException ex) {
                        throw new RuntimeException(ex);
                    }

                    if (result[0]) {
                        JSONFileUtils.put(ConstantPool.SETTINGS_USER_HAS_SIGN_IN, false, true);
                        Client.getClient().currentUser = null;
                        return true;
                    } else {
                        return false;
                    }
                }

                @Override
                public void doneSuccess() {
                    dialogResult = true;
                    closeWindow();
                }

                @Override
                public void doneFail() {
                    dialogResult = false;
                }
            };
            waitDialog.showWindow();
        });
        setCancelButton("取消");
        setCancelAction(e -> {
            dialogResult = false;
            closeWindow();
        });
    }
}
